package com.blog.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcj.blog.controller.ControllerUtil;
import com.tcj.blog.controller.DoThing;
import com.tcj.blog.domain.BlogLabel;
import com.tcj.blog.domain.BlogTopic;
import com.tcj.blog.domain.Classify;
import com.tcj.blog.domain.LoginConfig;
import com.tcj.blog.dto.JsonResult;
import com.tcj.blog.dto.ResponseCode;
import com.tcj.blog.service.*;
import com.tcj.blog.vo.MessageBoardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 2 * @Author: Tcj
 * 3 * @Date: 2020/11/17 15:48
 * 4
 */
@RestController
@CrossOrigin
@Api(tags = "公用数据接口")
public class DataController {
    @Autowired
    private IBlogLabelService labelService;
    @Autowired
    private IClassifyService classifyService;
    @Autowired
    private ILoginConfigService loginConfigService;
    @Autowired
    private IMessageBoardService messageBoardService;
    @Autowired
    private IBlogTopicService blogTopicService;
    @ApiOperation(value = "获取所有的标签")
    @PostMapping("/label")
    public JsonResult getLabel() {
        JsonResult jsonResult = new JsonResult();

        try {
            List<BlogLabel> list = labelService.list();
            jsonResult.setStatus(ResponseCode.OK);
            jsonResult.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setStatus(ResponseCode.ERROR);
            jsonResult.setMessage("未知错误，请联系管理员");
        }

        return jsonResult;
    }

    @PostMapping("/classify")
    @ApiOperation(value = "获取所有分类")
    public JsonResult getClassify() {
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                List<Classify> list = classifyService.list();
                jsonResult.setStatus(ResponseCode.OK);
                jsonResult.setData(list);
            }
        });
    }
    @PostMapping("/getConfig")
    @ApiOperation(value = "获取前端登录页面配置")
    public JsonResult getConfig(){
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                List<LoginConfig> list = loginConfigService.list();
                jsonResult.setStatus(ResponseCode.OK);
                jsonResult.setData(list.get(0));
            }
        });
    }
    @PostMapping("/topicDetail")
    @ApiOperation(value = "获取用户回复数据")
    public JsonResult getTopicDetailData(@RequestParam int pageIndex, @RequestParam int pageSize,@RequestParam int blogId){
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                IPage<MessageBoardVO> iPage = messageBoardService.listMessageBordDataByPage(pageIndex,pageSize,blogId);
                jsonResult.setData(iPage);
                jsonResult.setStatus(ResponseCode.OK);
            }
        });
    }
    @ApiOperation(value = "获取帖子详情数据")
    @GetMapping("/topicInfo/{id}")
    public JsonResult getTopicInfo(@PathVariable int id){
        if (id == 0){
            id = 1;
        }
        int finalId = id;
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                QueryWrapper<BlogTopic> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("id", finalId);
                BlogTopic blogTopic = blogTopicService.getOne(queryWrapper);
                if (blogTopic == null){
                    jsonResult.setStatus(ResponseCode.BAD_REQUEST);
                }else {
                    jsonResult.setStatus(ResponseCode.OK);
                    jsonResult.setData(blogTopic);
                }
            }
        });
    }

    @ApiOperation(value = "获取最新文章")
    @PostMapping("/newtopic/{pageIndex}")
    public JsonResult getNewTopic(@PathVariable int pageIndex){
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                Page<BlogTopic> page = new Page<>(pageIndex,3);
                QueryWrapper<BlogTopic> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc("create_time");
                Page<BlogTopic> result = blogTopicService.page(page, queryWrapper);
                jsonResult.setStatus(ResponseCode.OK);
                jsonResult.setData(result);
            }
        });
    }

    @ApiOperation(value = "根据分类获取帖子")
    @PostMapping("/topicByClassify")
    public JsonResult getTopicByClassify( int pageIndex,int pageSize,int id){
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                Page<BlogTopic> page = new Page<>(pageIndex,pageSize);
                QueryWrapper<BlogTopic> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("classify_id",id);
                Page<BlogTopic> result = blogTopicService.page(page, queryWrapper);
                jsonResult.setStatus(ResponseCode.OK);
                jsonResult.setData(result);
            }
        });
    }

    @ApiOperation(value = "根据标签获取帖子")
    @PostMapping("/topicByLabel")
    public JsonResult getTopicByLabel( int pageIndex,int pageSize,int id){
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                Page<BlogTopic> page = new Page<>(pageIndex,pageSize);
                QueryWrapper<BlogTopic> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("label_id",id);
                Page<BlogTopic> result = blogTopicService.page(page, queryWrapper);
                jsonResult.setStatus(ResponseCode.OK);
                jsonResult.setData(result);
            }
        });
    }

    @ApiOperation(value = "获取特别推荐帖子")
    @PostMapping("/getRecommend")
    public JsonResult getRecommendTopic(){
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                Page<BlogTopic> page = new Page<>(1,9);
                QueryWrapper<BlogTopic> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc("weight");
                Page<BlogTopic> result = blogTopicService.page(page, queryWrapper);
                jsonResult.setStatus(ResponseCode.OK);
                jsonResult.setData(result);
            }
        });

    }

    @ApiOperation(value = "获取点击排行数据")
    @PostMapping("/getClickList")
    public JsonResult getClickList(){
        return ControllerUtil.getResult(new DoThing() {
            @Override
            public void doThing(JsonResult jsonResult) {
                Page<BlogTopic> page = new Page<>(1,5);
                QueryWrapper<BlogTopic> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc("view");
                Page<BlogTopic> result = blogTopicService.page(page, queryWrapper);
                jsonResult.setData(result);
                jsonResult.setStatus(ResponseCode.OK);
            }
        });
    }
}